// pages/find/find.js
const api = require("../../libs/api");
var dateTimePicker = require('../../utils/dateTimePicker.js');
import {
  formatDate
} from '../../utils/util.js';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    timslit:'',
    a: 1,
    isaut: false,//授权状态
    isRemove: false,  //是否删除成功
    lifenotepad: [],//分类的内容
    worknotepad: [],
    othernotepad: [],
    studynotepad: [],
    lifeid: "0bcbdde05fca53bf00ae7e6d20b44837",//生活id
    workid: "e62469b25fca54a700deae655351d68d",
    otherid: "0288fce75fca551100b7d78531a49c62",
    studyid: "0bcbdde05fca54e500ae861f506b7f8d",
    menuType: [],//存放分类类别
    islogin: false,
    currentTab: 0,
    winWidth: 0,
    winHeight: 0,

    //下拉时间选项初始化
    startYear: 2019,
    endYear: 2090,
    startDateArray: null,
    startTime: null

  },
  /****swipe处理*****/
  //滑动切换   
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current,
    });
  },
  //点击切换  
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        id: e.target.dataset.id
      })
      // console.log(e.target.dataset.id)
    }

  },
  /******************** */
  //按钮事件   跳页面
  gotoAdd() {
    if (!app.getLoginStatus()) {
      app.msg('你还没有登录呢')
      return
    } else {
      wx.showLoading({
        title: '正在跳转',
        mask: true
      })
      setTimeout(function () {
        wx.hideLoading()
        wx.navigateTo({
          url: '../add/add',
        })
      }, 500)

    }

  },


  onLoad: function (options) {
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // var startDate = this.formatDate(new Date());
    this.setData({
      startDate: obj.dateTime,
      startDateArray: obj.dateTimeArray,
      endDate: obj.dateTime,
      endDateArray: obj.dateTimeArray
    })
    this.Timechange()
  },


  onShow: function () {

    var that = this;
    that.getmenuDetaile();
    that.onLoad();
    that.getMenuType();
    if (app.getLoginStatus()) {
      wx.showLoading({
        title: '加载中',
      })
      setTimeout(function () {
        wx.hideLoading()
        let userinfo = wx.getStorageSync('userinfo');
        that.setData({
          islogin: true,
        })
      }, 500)
    } else {
      console.log("tttt")
      that.setData({
        islogin: false,
      })
    }


  },

  //获取所有分类类型
  getMenuType() {
    api.find("notepadType").then(res => {
      this.setData({
        menuType: res.data
      })
    })
  },


  //获取当前类的所有记事
  getmenuDetaile() {
    let openid = wx.getStorageSync('openid');
    let lifeid = this.data.lifeid;
    let workid = this.data.workid;
    let otherid = this.data.otherid;
    let studyid = this.data.studyid;
    api.find("notepads", { notepadTypeid: lifeid, _openid: openid }).then(res => {
      console.log(res.data)
      this.setData({
        lifenotepad: res.data
      })
    })
    api.find("notepads", { notepadTypeid: workid, _openid: openid }).then(res => {
      console.log(res.data)
      this.setData({
        worknotepad: res.data
      })
    })
    api.find("notepads", { notepadTypeid: otherid, _openid: openid }).then(res => {
      console.log(res.data)
      this.setData({
        othernotepad: res.data
      })
    })
    api.find("notepads", { notepadTypeid: studyid, _openid: openid }).then(res => {
      console.log(res.data)
      this.setData({
        studynotepad: res.data
      })
    })
  },

  //去详情页面
  toDetails(event) {
    let { id } = event.currentTarget.dataset;
    // console.log(event.currentTarget.dataset)
    wx.navigateTo({
      url: `/pages/noteDetails/noteDetails?id=${id}`
    })
  },

  //删除事件
  removenotepad: function (e) {
    let { id } = e.currentTarget.dataset;
    // console.log(e.currentTarget.dataset)
    let openid = wx.getStorageSync('openid');
    api.callfun("remove", {
      collectionName: "notepads",
      where: {
        _id: id,
        _openid: openid
      }
    }).then(res => {
      this.setData({
        isRemove: true
      })
      wx.showToast({
        title: '删除成功',
      })
      this.getmenuDetaile();
    })
    console.log("你点了删除")
  },


  //滑动事件处理
  touchmove: function (e) {
    let a = this.data.a
    a++
    var that = this,
      index = e.currentTarget.dataset.index;//当前索引
    switch (this.data.currentTab) {
      case 0: that.data.lifenotepad.forEach(function (v, i) {
        v.isTouchMove = false
        if (i == index) {
          if (a % 2 == 0) { //右滑
            v.isTouchMove = true
            that.setData({
              a
            })
          }
          else { //左滑
            v.isTouchMove = false
            that.setData({
              a
            })
          }
        }
      });
        break;
      case 1: that.data.worknotepad.forEach(function (v, i) {
        v.isTouchMove = false
        if (i == index) {
          if (a % 2 == 0) { //右滑
            v.isTouchMove = true
            that.setData({
              a
            })
          }
          else { //左滑
            v.isTouchMove = false
            that.setData({
              a
            })
          }
        }
      });
        break;
      case 3: that.data.othernotepad.forEach(function (v, i) {
        v.isTouchMove = false
        if (i == index) {
          if (a % 2 == 0) { //右滑
            v.isTouchMove = true
            that.setData({
              a
            })
          }
          else { //左滑
            v.isTouchMove = false
            that.setData({
              a
            })
          }
        }
      });
        break;
      case 2: that.data.studynotepad.forEach(function (v, i) {
        v.isTouchMove = false
        if (i == index) {
          if (a % 2 == 0) { //右滑
            v.isTouchMove = true
            that.setData({
              a
            })
          }
          else { //左滑
            v.isTouchMove = false
            that.setData({
              a
            })
          }
        }
      });
        break;
    }
    //更新数据
    that.setData({
      lifenotepad: this.data.lifenotepad,
      worknotepad: this.data.worknotepad,
      studynotepad: this.data.studynotepad,
      othernotepad: this.data.othernotepad
    })
  },

  bindStartDateChange(e) {
    console.log(e.detail)
    this.setData({
      startDate: e.detail.value,
    })
    this.Timechange();
    // this.shouquan(e.currentTarget.dataset)
    console.log(e.currentTarget.dataset)
    let _id = e.currentTarget.dataset.id;
    let state = e.currentTarget.dataset.state;
        if (true) {
          console.log(this.data.timslit)
          wx.showLoading({
            title: '正在修改',
          })
          if (!state) {
            api.callfun("updata", {
              where: { _id: _id },
              data: {
                state: true,
                stateid: 'e62469b25fc241db008200c3013dcb25',
                reminde: this.data.timslit
              },
              collectionName: "notepads"
            }).then(res => {
              wx.hideLoading()
              wx.showToast({
                title: '修改成功',
              })
              this.getmenuDetaile();
            }).catch(err => {
              wx.showToast({
                title: '修改失败',
              })
            })
          } else {
            api.callfun("updata", {
              where: { _id: _id },
              data: {
                state: false,
                stateid: '2a7b532a5fc24202007e446670b02cc7',
                reminde: this.data.timslit
              },
              collectionName: "notepads"
            }).then(res => {
              wx.hideLoading()
              wx.showToast({
                title: '修改成功',
              })
              this.getmenuDetaile();
            }).catch(err => {
              wx.showToast({
                title: '修改失败',
              })
            })
      
          }
        }
    
  },
  //获取设置的时间
  Timechange() {
    let startDateArray = this.data.startDateArray;
    let startDate = this.data.startDate;
    let timslit = startDateArray[0][startDate[0]] + '-' + startDateArray[1][startDate[1]] + '-' + startDateArray[2][startDate[2]] + ' ' + startDateArray[3][startDate[3]] + ':' + startDateArray[4][startDate[4]]
    this.setData({
      timslit: timslit
    })
  },


  shouquan(e) {
    wx.requestSubscribeMessage({
      tmplIds: ['GchwxG9wH3_F0wiVy9IP1lP7l8aqr1ViKyMnXIwcbag'],
      success(res) {
          console.log("授权成功", res);
        
      }, fail(err) {
        console.log("授权失败", err);
      }
    })




   



  },
  //用户授权
  // async shouquan(e) {
  //   let { title, center } = e.currentTarget.dataset;
  //   let openid = wx.getStorageSync('openid');
  //   wx.requestSubscribeMessage({
  //     tmplIds: ['GchwxG9wH3_F0wiVy9IP1lP7l8aqr1ViKyMnXIwcbag'],
  //     success(res) {
  //       if(res.errMsg === 'requestSubscribeMessage:ok'){
  //         console.log('订阅成功', res)
  //       }
  //     }, fail(err) {
  //       console.log("授权失败", err);
  //     }
  //   })

  //   //发送推送消息
  //   await api.callfun("fasong", {
  //     openid: openid,
  //     center: center,
  //     title: title
  //   }).then(res => {
  //     console.log(res)
  //   }).catch(err => {
  //     console.log(err)
  //   })


  //   this.setData({
  //     state:true
  //   })
  // },








  //用户点击右上角分享
  onShareAppMessage: function () {

  }
})